package nuc.wy;

import java.util.ArrayList;

import nuc.SQLoperation.Constant;
import nuc.SQLoperation.SQLiteOperationHelper;
import nuc.widget.Parse;
import nuc.widget.WidgetActivity;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class CreateNote extends Activity {

	private Button confirmButton;
	private EditText titleEditText;
	private EditText contentEditText;
	private ImageButton voiceInputButton;
	private ArrayList<String> result;
	String backResult=null;
	
	private static final int RECOGNIZER_EXAMPLE = 1001;
	
	Long id = (long) 0;
	SimpleNote note ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.createnote);
		titleEditText = (EditText) findViewById(R.id.TitleEditTextId);
		contentEditText = (EditText) findViewById(R.id.ContentEditTextId);

////<=======接收修改
		Intent intentPre = getIntent();
		if(intentPre.getLongExtra("id", 0) != 0){
			id = intentPre.getLongExtra("id", 0);
			SQLiteOperationHelper DBHelper = new SQLiteOperationHelper(	CreateNote.this, Constant.DB_NAME, null, 1);
			Cursor cursor = DBHelper.queryByid(id.toString());
			cursor.moveToFirst();
			note = Parse.parseCursor(cursor);
			titleEditText.setText(note.title);
			contentEditText.setText(note.content);
			DBHelper.close();
		}
////=====>游标

		confirmButton = (Button) findViewById(R.id.ConfirmButtonId);
		confirmButton.setOnClickListener(new CreateNoteListener());
		
		voiceInputButton = (ImageButton) findViewById(R.id.VoiceInputButtonId);
		voiceInputButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try{
					Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
					intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
					intent.putExtra(RecognizerIntent.EXTRA_PROMPT, R.string.voice);
					startActivityForResult(intent, RECOGNIZER_EXAMPLE);
				}
				catch(ActivityNotFoundException e){
					//找不到语音设备
					Toast.makeText(CreateNote.this, R.string.voice_equipment_notfound, Toast.LENGTH_LONG).show();
				}
			}
		});
		voiceInputButton.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction()==MotionEvent.ACTION_DOWN){
					voiceInputButton.setBackgroundResource(R.drawable.voiceset);
				}
				else {
					voiceInputButton.setBackgroundResource(R.drawable.voice);
				}
				return false;
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(requestCode == RECOGNIZER_EXAMPLE && resultCode == RESULT_OK){
			result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//			System.out.println(result.toString());
			ListView listview = new ListView(this);
			listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, result));
			setContentView(listview);
			listview.setOnItemClickListener(new ListViewListener());
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
	class CreateNoteListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String title = titleEditText.getText().toString();
			String content = contentEditText.getText().toString();
			
			if (!content.equals("")) {
				ContentValues values = new ContentValues();
				String Date = Long.toString(System.currentTimeMillis());
				values.put("_title", title);
				values.put("_content", content);
				values.put("_lastdate", Date);
				SQLiteOperationHelper DBHelper = new SQLiteOperationHelper(
						CreateNote.this, Constant.DB_NAME, null, 1);
				if (id==0 && DBHelper.insert(values)) {
					//更新appwidget
					WidgetActivity widget  = new WidgetActivity();
					widget.update(getApplicationContext());
					//提示更新成功
					Toast.makeText(CreateNote.this, R.string.add_succeed,
							Toast.LENGTH_SHORT).show();
					finish();
				}
				if (id != 0 && DBHelper.update(Long.parseLong(note.id),values)) {
					//更新appwidget
					WidgetActivity widget  = new WidgetActivity();
					widget.update(getApplicationContext());
					//提示更新成功
					Toast.makeText(CreateNote.this, R.string.update_succeed,
							Toast.LENGTH_SHORT).show();
					finish();
				}
				DBHelper.close();
			} else
				Toast.makeText(CreateNote.this, R.string.add_error,
						Toast.LENGTH_SHORT).show();
		}
	}
	
	class ListViewListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			System.out.println(result.get(arg2));
			contentEditText.setText(result.get(arg2));
			CreateNoteListener voiceCreate = new CreateNoteListener();
			voiceCreate.onClick(arg0);
		}
	}
}
